<template>
  <div>
    <el-card class="category-container">
      <template #header>
        <div class="header">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">增加</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="el-icon-delete">批量删除</el-button>
            </template>
          </el-popconfirm>
          <el-divider></el-divider>
          <el-card class="account-container">
            <el-form :model="searchForm" :rules="rules" ref="nameRef" label-width="100px" :inline="true" label-position="right" class="demo-ruleForm">
              <el-form-item label="小项名称：" prop="projectInfo">
                <el-input style="width: 200px" v-model="searchForm.projectInfo"></el-input>
              </el-form-item>
              <el-form-item label="项目名称：" prop="projectName">
                <el-input style="width: 200px" v-model="searchForm.projectName"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="danger" @click="searchFormFun">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </div>
      </template>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="projectInfo"
          label="分类名称"
        >
        </el-table-column>
        <el-table-column
          prop="projectNumber"
          label="排序值"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          width="200"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="120"
        >
          <template #default="scope">
            <a style=" margin-right: 10px; cursor: pointer;" @click="handleEdit(scope.row.categoryId)">修改</a>
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDeleteOne(scope.row.categoryId)"
            >
              <template #reference>
                <a style="cursor: pointer">删除</a>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--总数超过一页，再展示分页器-->
      <page :pageInfo="pageInfo" @reload="getCategory" />
    </el-card>
    <DialogAddCategory ref='addGood' :reload="getCategory" :type="type"/>
  </div>
</template>

<script>
import { onMounted, onUnmounted, reactive, ref, toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import DialogAddCategory from '@/components/DialogAddCategory.vue'
import page from '@/components/page.vue'
// import axios from '@/utils/axios'
import commonApi from "@/api/commonApi";
import { localGet, localSet } from '@/utils'
export default {
  name: 'Category',
  components: {
    DialogAddCategory,
    page
  },
  setup() {
    const multipleTable = ref(null)
    const addGood = ref(null)
    const router = useRouter()
    const route = useRoute()
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      searchForm: localGet(route.name+'searchForm') || {},
      pageInfo: {
        total: 0, // 总条数
        pageNo: localGet(route.name+'pageNo') || 1, // 当前页
        pageSize: localGet(route.name+'pageSize') || 10, // 分页大小
      },
      rules: {
        // projectInfo: { required: true }
      },
      type: 'add', // 操作类型
      level: 1,
      parent_id: 0
    })
    onMounted(() => {
      getCategory()
    })
    onUnmounted(() => {
      unwatch()
    })
    const unwatch = router.afterEach((to) => {
      if (['category', 'level2', 'level3'].includes(to.name)) {
        getCategory()
      }
    })
    // 获取分类列表
    const getCategory = async (tag) => {
      state.loading = true
      let res = await commonApi.pageDBJSJSS({
        data: {
          flowCode: "SJSS",
          node: "sjss_start",
          type: 0,
          year: "2021",
          ...state.searchForm
        },
        ...(tag || state.pageInfo)
      });
      if (res.data.code == 0) {
        let data = res.data.data
        state.tableData = data.records
        state.pageInfo.pageNo = Number(data.current)
        state.pageInfo.total = Number(data.total)
        state.loading = false
      }
    }
    // 添加分类
    const handleAdd = () => {
      state.type = 'add'
      addGood.value.open()
    }
    // 修改分类
    const handleEdit = (id) => {
      state.type = 'edit'
      // addGood.value.open(id)
      router.push({ name: 'level' })
    }
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
    // 批量删除
    const handleDelete = () => {
      if (!state.multipleSelection.length) {
        ElMessage.error('请选择项')
        return
      }
      axios.delete('/categories', {
        data: {
          ids: state.multipleSelection.map(i => i.categoryId)
        }
      }).then(() => {
        ElMessage.success('删除成功')
        getCategory()
      })
    }
    // 单个删除
    const handleDeleteOne = (id) => {
      axios.delete('/categories', {
        data: {
          ids: [id]
        }
      }).then(() => {
        ElMessage.success('删除成功')
        getCategory()
      })
    }
    const searchFormFun = ()=> {
      localSet(route.name+'searchForm', state.searchForm);
      getCategory();
    }
    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      addGood,
      handleAdd,
      handleEdit,
      handleDelete,
      handleDeleteOne,
      getCategory,
      searchFormFun
    }
  }
}
</script>

<style scoped>
  .category-container {
    min-height: 100%;
  }
  .el-card.is-always-shadow {
    min-height: 100%!important;
  }
</style>